﻿// ======================================================================
//
//           filename : PdfExporter_Tests.cs
//           description :
//
//           created by 雪雁 at  2019-09-26 14:59
//           文档官网：https://docs.xin-lai.com
//           公众号教程：麦扣聊技术
//           QQ群：85318032（编程交流）
//           Blog：http://www.cnblogs.com/codelove/
//
// ======================================================================

using System;
using System.Collections.Generic;
using System.IO;
using System.Threading.Tasks;
using Magicodes.ExporterAndImporter.Pdf;
using Magicodes.ExporterAndImporter.Tests.Models.Export;
using Shouldly;
using WkHtmlToPdfDotNet;
using Xunit;

namespace Magicodes.ExporterAndImporter.Tests
{
    public class PdfExporter_Tests : TestBase
    {
        [Fact(DisplayName = "导出竖向排版收据")]
        public async Task BathExportPortraitReceipt_Test()
        {
            var tplPath = Path.Combine(Directory.GetCurrentDirectory(), "TestFiles", "ExportTemplates",
                "batchReceipt.cshtml");
            var tpl = File.ReadAllText(tplPath);
            var exporter = new PdfExporter();
            var filePath = Path.Combine(Directory.GetCurrentDirectory(),
                nameof(BathExportPortraitReceipt_Test) + ".pdf");
            if (File.Exists(filePath)) File.Delete(filePath);

            var input = new BatchPortraitReceiptInfoInput
            {
                Payee = "湖南心莱信息科技有限公司",
                SealUrl =
                    @"",
                LogoUrl =
                    @"",
                ReceiptInfoInputs = new List<BatchPortraitReceiptInfoDto>()
            };

            for (var i = 0; i < 500; i++)
                input.ReceiptInfoInputs.Add(new BatchPortraitReceiptInfoDto
                {
                    Amount = 22939.43M,
                    Grade = "2019秋",
                    IdNo = "43062619890622xxxx",
                    Name = "张三",
                    PaymentMethod = "微信支付",
                    Profession = "运动训练",
                    Remark = "学费",
                    TradeStatus = "已完成",
                    TradeTime = DateTime.Now,
                    UppercaseAmount = "贰万贰仟玖佰叁拾玖圆肆角叁分",
                    Code = "1907180000" + i
                });

            //此处使用默认模板导出
            var result = await exporter.ExportByTemplate(filePath, input, tpl);
            result.ShouldNotBeNull();
            File.Exists(filePath).ShouldBeTrue();
        }

        [Fact(DisplayName = "导出竖向排版收据Type类型测试")]
        public async Task BathExportPortraitReceipt_Type_Test()
        {
            var tplPath = Path.Combine(Directory.GetCurrentDirectory(), "TestFiles", "ExportTemplates",
                "batchReceipt.cshtml");
            var tpl = File.ReadAllText(tplPath);
            var exporter = new PdfExporter();
            var filePath = Path.Combine(Directory.GetCurrentDirectory(),
                nameof(BathExportPortraitReceipt_Test) + ".pdf");
            if (File.Exists(filePath)) File.Delete(filePath);

            var input = new BatchPortraitReceiptInfoInput
            {
                Payee = "湖南心莱信息科技有限公司",
                SealUrl =
                    @"",
                LogoUrl =
                    @"",
                ReceiptInfoInputs = new List<BatchPortraitReceiptInfoDto>()
            };

            for (var i = 0; i < 500; i++)
                input.ReceiptInfoInputs.Add(new BatchPortraitReceiptInfoDto
                {
                    Amount = 22939.43M,
                    Grade = "2019秋",
                    IdNo = "43062619890622xxxx",
                    Name = "张三",
                    PaymentMethod = "微信支付",
                    Profession = "运动训练",
                    Remark = "学费",
                    TradeStatus = "已完成",
                    TradeTime = DateTime.Now,
                    UppercaseAmount = "贰万贰仟玖佰叁拾玖圆肆角叁分",
                    Code = "1907180000" + i
                });

            //此处使用默认模板导出
            var result = await exporter.ExportBytesByTemplate(input, tpl, typeof(BatchPortraitReceiptInfoInput));
            result.ShouldNotBeNull();
            using (var file = File.OpenWrite(filePath))
            {
                file.Write(result, 0, result.Length);
            }

            File.Exists(filePath).ShouldBeTrue();
        }

        [Fact(DisplayName = "批量导出收据")]
        public async Task BathExportReceipt_Test()
        {
            var tplPath = Path.Combine(Directory.GetCurrentDirectory(), "TestFiles", "ExportTemplates",
                "batchReceipt.cshtml");
            var tpl = File.ReadAllText(tplPath);
            var exporter = new PdfExporter();
            var filePath = Path.Combine(Directory.GetCurrentDirectory(), nameof(BathExportReceipt_Test) + ".pdf");
            if (File.Exists(filePath)) File.Delete(filePath);

            var input = new BatchReceiptInfoInput
            {
                Payee = "湖南心莱信息科技有限公司",
                SealUrl =
                    @"",
                LogoUrl =
                    @"",
                ReceiptInfoInputs = new List<BatchReceiptInfoDto>()
            };

            for (var i = 0; i < 500; i++)
                input.ReceiptInfoInputs.Add(new BatchReceiptInfoDto
                {
                    Amount = 22939.43M,
                    Grade = "2019秋",
                    IdNo = "43062619890622xxxx",
                    Name = "张三",
                    PaymentMethod = "微信支付",
                    Profession = "运动训练",
                    Remark = "学费",
                    TradeStatus = "已完成",
                    TradeTime = DateTime.Now,
                    UppercaseAmount = "贰万贰仟玖佰叁拾玖圆肆角叁分",
                    Code = "1907180000" + i
                });

            //此处使用默认模板导出
            var result = await exporter.ExportByTemplate(filePath, input, tpl);
            result.ShouldNotBeNull();
            File.Exists(filePath).ShouldBeTrue();

            input.ReceiptInfoInputs = new List<BatchReceiptInfoDto>();
            for (var i = 0; i < 50; i++)
            {
                input.ReceiptInfoInputs.Add(new BatchReceiptInfoDto
                {
                    Amount = i,
                    Grade = "2019秋",
                    IdNo = "43062619890622xxxx",
                    Name = "张三",
                    PaymentMethod = "支付宝支付",
                    Profession = "运动训练",
                    Remark = "学费",
                    TradeStatus = "已完成",
                    TradeTime = DateTime.Now,
                    UppercaseAmount = "贰万贰仟玖佰叁拾玖圆肆角叁分",
                    Code = "1907180000" + i
                });
                filePath = Path.Combine(Directory.GetCurrentDirectory(), nameof(BathExportReceipt_Test) + i + ".pdf");
                if (File.Exists(filePath)) File.Delete(filePath);
                result = await exporter.ExportByTemplate(filePath, input, tpl);
                result.ShouldNotBeNull();
                File.Exists(filePath).ShouldBeTrue();
            }
        }

        [Fact(DisplayName = "导出Pdf测试")]
        public async Task ExportPdf_Test()
        {
            var exporter = new PdfExporter();
            var filePath = Path.Combine(Directory.GetCurrentDirectory(), nameof(ExportPdf_Test) + ".pdf");
            if (File.Exists(filePath)) File.Delete(filePath);
            //此处使用默认模板导出
            var result = await exporter.ExportListByTemplate(filePath, GenFu.GenFu.ListOf<ExportTestData>());
            result.ShouldNotBeNull();
            File.Exists(filePath).ShouldBeTrue();
        }

        [Fact(DisplayName = "自定义模板导出Pdf测试")]
        public async Task ExportPdfByTemplate_Test()
        {
            var tplPath = Path.Combine(Directory.GetCurrentDirectory(), "TestFiles", "ExportTemplates", "tpl1.cshtml");
            var tpl = File.ReadAllText(tplPath);
            var exporter = new PdfExporter();
            var filePath = Path.Combine(Directory.GetCurrentDirectory(), nameof(ExportPdfByTemplate_Test) + ".pdf");
            if (File.Exists(filePath)) File.Delete(filePath);
            //此处使用默认模板导出
            var result = await exporter.ExportListByTemplate(filePath,
                GenFu.GenFu.ListOf<ExportTestData>(), tpl);
            result.ShouldNotBeNull();
            File.Exists(filePath).ShouldBeTrue();
        }

        [Fact(DisplayName = "自定义模板导出PdfBytes测试")]
        public async Task ExportPdfBytesByTemplate_Test()
        {
            var tplPath = Path.Combine(Directory.GetCurrentDirectory(), "TestFiles", "ExportTemplates", "tpl1.cshtml");
            var tpl = File.ReadAllText(tplPath);
            var exporter = new PdfExporter();
            var filePath = Path.Combine(Directory.GetCurrentDirectory(),
                nameof(ExportPdfBytesByTemplate_Test) + ".pdf");
            if (File.Exists(filePath)) File.Delete(filePath);
            //此处使用默认模板导出
            var result = await exporter.ExportListBytesByTemplate(
                GenFu.GenFu.ListOf<ExportTestData>(), tpl);
            result.ShouldNotBeNull();
            using (var file = File.OpenWrite(filePath))
            {
                file.Write(result, 0, result.Length);
            }

            File.Exists(filePath).ShouldBeTrue();
        }

        [Fact(DisplayName = "导出收据")]
        public async Task ExportReceipt_Test()
        {
            var tplPath = Path.Combine(Directory.GetCurrentDirectory(), "TestFiles", "ExportTemplates",
                "receipt.cshtml");
            var tpl = File.ReadAllText(tplPath);
            var exporter = new PdfExporter();
            var filePath = Path.Combine(Directory.GetCurrentDirectory(), nameof(ExportReceipt_Test) + ".pdf");
            if (File.Exists(filePath)) File.Delete(filePath);
            //此处使用默认模板导出
            var result = await exporter.ExportByTemplate(filePath,
                new ReceiptInfo
                {
                    Amount = 22939.43M,
                    Grade = "2019秋",
                    IdNo = "43062619890622xxxx",
                    Name = "张三",
                    Payee = "湖南心莱信息科技有限公司",
                    PaymentMethod = "微信支付",
                    Profession = "运动训练",
                    Remark = "学费",
                    TradeStatus = "已完成",
                    TradeTime = DateTime.Now,
                    UppercaseAmount = "贰万贰仟玖佰叁拾玖圆肆角叁分",
                    Code = "19071800001"
                }, tpl);
            result.ShouldNotBeNull();
            File.Exists(filePath).ShouldBeTrue();

            filePath = Path.Combine(Directory.GetCurrentDirectory(), nameof(ExportReceipt_Test) + "1.pdf");
            result = await exporter.ExportByTemplate(filePath,
                new ReceiptInfo
                {
                    Amount = 22939.43M,
                    Grade = "2019秋",
                    IdNo = "43062619890622xxxx",
                    Name = "张三",
                    Payee = "湖南心莱信息科技有限公司",
                    PaymentMethod = "微信支付",
                    Profession = "运动训练",
                    Remark = "学费",
                    TradeStatus = "已完成",
                    TradeTime = DateTime.Now,
                    UppercaseAmount = "贰万贰仟玖佰叁拾玖圆肆角叁分",
                    Code = "19071800001"
                }, tpl);
            result.ShouldNotBeNull();
            File.Exists(filePath).ShouldBeTrue();

            filePath = Path.Combine(Directory.GetCurrentDirectory(), nameof(ExportReceipt_Test) + "2.pdf");
            result = await exporter.ExportByTemplate(filePath,
                new ReceiptInfo
                {
                    Amount = 22939.43M,
                    Grade = "2019秋",
                    IdNo = "43062619890622xxxx",
                    Name = "张三",
                    Payee = "湖南心莱信息科技有限公司",
                    PaymentMethod = "微信支付",
                    Profession = "运动训练",
                    Remark = "学费",
                    TradeStatus = "已完成",
                    TradeTime = DateTime.Now,
                    UppercaseAmount = "贰万贰仟玖佰叁拾玖圆肆角叁分",
                    Code = "19071800001"
                }, tpl);
            result.ShouldNotBeNull();
            File.Exists(filePath).ShouldBeTrue();
        }

        [Fact(DisplayName = "根据模板以及特性参数导出PdfBytes测试")]
        public async Task ExportPdfBytesByTemplateAndAttr_Test()
        {
            var tplPath = Path.Combine(Directory.GetCurrentDirectory(), "TestFiles", "ExportTemplates", "tpl1.cshtml");
            var tpl = File.ReadAllText(tplPath);
            var exporter = new PdfExporter();
            var filePath = Path.Combine(Directory.GetCurrentDirectory(),
                nameof(ExportPdfBytesByTemplate_Test) + ".pdf");
            if (File.Exists(filePath)) File.Delete(filePath);
            //此处使用默认模板导出
            var result = await exporter.ExportListBytesByTemplate(
                GenFu.GenFu.ListOf<ExportTestData>(), new PdfExporterAttribute()
                {
                    PaperKind = PaperKind.A4,
                    IsEnablePagesCount = true,
                    HeaderSettings = new HeaderSettings() { FontSize = 9, Right = "Page [page] of [toPage]", Line = true, Spacing = 2.812 }
                }, tpl);
            result.ShouldNotBeNull();
            using (var file = File.OpenWrite(filePath))
            {
                file.Write(result, 0, result.Length);
            }

            File.Exists(filePath).ShouldBeTrue();
        }

        [Fact(DisplayName = "特性参数导出收据")]
        public async Task ExportReceiptByAttr_Test()
        {
            var tplPath = Path.Combine(Directory.GetCurrentDirectory(), "TestFiles", "ExportTemplates",
                "receipt.cshtml");
            var tpl = File.ReadAllText(tplPath);
            var exporter = new PdfExporter();
            var filePath = Path.Combine(Directory.GetCurrentDirectory(), nameof(ExportReceipt_Test) + ".pdf");
            if (File.Exists(filePath)) File.Delete(filePath);
            //此处使用默认模板导出
            var result = await exporter.ExportBytesByTemplate(
                new ReceiptInfo
                {
                    Amount = 22939.43M,
                    Grade = "2019秋",
                    IdNo = "43062619890622xxxx",
                    Name = "张三",
                    Payee = "湖南心莱信息科技有限公司",
                    PaymentMethod = "微信支付",
                    Profession = "运动训练",
                    Remark = "学费",
                    TradeStatus = "已完成",
                    TradeTime = DateTime.Now,
                    UppercaseAmount = "贰万贰仟玖佰叁拾玖圆肆角叁分",
                    Code = "19071800001"
                }, new PdfExporterAttribute(), tpl);
            result.ShouldNotBeNull();
            using (var file = File.OpenWrite(filePath))
            {
                file.Write(result, 0, result.Length);
            }

            File.Exists(filePath).ShouldBeTrue();
        }
    }
}